package com.cjf.base.activity

import com.cjf.base.permission.PermissionUtils
import hos.permission.PermissionX


/**
 * <p>Title: AndActivity </p>
 * <p>Description:  </p>
 * <p>Company: www.mapuni.com </p>
 *
 * @author : 蔡俊峰
 * @date : 2020/8/12 13:25
 * @version : 1.0
 */
abstract class PermissionXActivity : BaseActivity() {

    private var permissions: Array<String>? = null

    // 初始化
    private val permissionX = PermissionX.init(this)

    /**
     * Request permissions.
     */
    fun requestPermission(permissions: Array<String>) {
        this.permissions = permissions
        // 获取需要申请的权限
        val needPermissionList: List<String> =
            PermissionUtils.getNeedPermissionList(this, permissions)
        if (needPermissionList.isEmpty()) {
            // 没有需要申请的权限，不需要再进行申请了
            onGrantedSuccess()
            return
        }
        if (PermissionX.hasPermissions(this, permissions)) {
            onGrantedSuccess()
            return
        }
        permissionX
            .permissions(permissions.asList())
            .defaultDialog()
            .onCancel {
                onPermissionXCancel()
            }
            .request {
                onGrantedSuccess()
            }
    }

    abstract fun onGrantedSuccess()

    open fun onPermissionXCancel() {

    }

}